<template><div><h2 id="如何将不同的实现类注入到对象中" tabindex="-1"><a class="header-anchor" href="#如何将不同的实现类注入到对象中"><span>如何将不同的实现类注入到对象中</span></a></h2>
<p>通过@Resouce注解依名注入</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo01.controller</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.bind.annotation.RequestMapping</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.web.bind.annotation.RestController</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> xyz.hmz.springbootdemo01.service.ServiceInterface</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> javax.annotation.Resource</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/11 10:26</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description:</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">RestController</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> TestController</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Resource</span><span style="color:#E06C75">(</span><span style="color:#D19A66">name</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "serviceA"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> ServiceInterface</span><span style="color:#E06C75"> serviceA</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Resource</span><span style="color:#E06C75">(</span><span style="color:#D19A66">name</span><span style="color:#56B6C2"> =</span><span style="color:#98C379"> "serviceB"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    private</span><span style="color:#E5C07B"> ServiceInterface</span><span style="color:#E06C75"> serviceB</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">RequestMapping</span><span style="color:#E06C75">(</span><span style="color:#98C379">"/testA"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> testA</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#E5C07B">        serviceA</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">doSomething</span><span style="color:#ABB2BF">();</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">RequestMapping</span><span style="color:#E06C75">(</span><span style="color:#98C379">"/testB"</span><span style="color:#E06C75">)</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> testB</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#E5C07B">        serviceB</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">doSomething</span><span style="color:#ABB2BF">();</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h2 id="service-configuration-repository-component有什么区别-他们的本质是什么-这些注解创建的bean和-bean声明的bean有什么区别" tabindex="-1"><a class="header-anchor" href="#service-configuration-repository-component有什么区别-他们的本质是什么-这些注解创建的bean和-bean声明的bean有什么区别"><span>@Service,@Configuration,@Repository,@Component有什么区别，他们的本质是什么，这些注解创建的bean和@Bean声明的Bean有什么区别</span></a></h2>
<p>@Service, @Controller , @Repository , @Configuration = {@Component + 一些特定的功能},本质上都是交给springboot进行实例化，更多的作用是提醒开发着这个类的作用是什么，降低耦合性。对应注解会交由对应的框架处理。</p>
<p>这些注解创建的bean是通过反射api创建的，而@Bean是通过注解在方法上，接收方法的返回值，在方法内部，需要手动new创建对象。</p>
<p>本质上springboot会扫描所有@Service等等这种注解所标注的类，通过反射的方式创建对象，并交由springboot托管而不需要人为去操控对象的创建与销毁</p>
<h2 id="springbootbean创建" tabindex="-1"><a class="header-anchor" href="#springbootbean创建"><span>springbootBean创建</span></a></h2>
<ol>
<li>获取BeanDefinition</li>
<li>创建Bean对象</li>
<li>Bean对象属性注入(循环依赖使用三级缓存解决)</li>
<li>检查是否实现Aware接口，对应设置相关依赖</li>
<li>BeanPostProcessor前置处理</li>
<li>InitializingBean中置处理</li>
<li>是否有自定义初始化方法</li>
<li>后置处理</li>
<li>注册Destruction相关回调接口（销毁后执行的方法）</li>
<li>使用中</li>
<li>实现DisposableBean接口,自定义销毁方法</li>
</ol>
<h2 id="实验" tabindex="-1"><a class="header-anchor" href="#实验"><span>实验</span></a></h2>
<p>写一个Demo，定义一个Service的两个实现类A与B，要求通过对Bean定义或生命周期的操作，完成注解上注入A实际上注入B，注解注入B实际上注入A（埋坑大法）</p>
<div class="language-java line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="java" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">package</span><span style="color:#C678DD"> xyz.hmz.springbootdemo01.config</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.BeansException</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.config.BeanDefinition</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.config.ConfigurableListableBeanFactory</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.support.BeanDefinitionRegistry</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">import</span><span style="color:#E5C07B"> org.springframework.context.annotation.Configuration</span><span style="color:#ABB2BF">;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic">/**</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Author haomingze</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @Date 2025/3/22 09:34</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> * @description:</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic"> */</span></span>
<span class="line"><span style="color:#ABB2BF">@</span><span style="color:#E5C07B">Configuration</span></span>
<span class="line"><span style="color:#C678DD">public</span><span style="color:#C678DD"> class</span><span style="color:#E5C07B"> BeanSwapConfig</span><span style="color:#C678DD"> implements</span><span style="color:#E5C07B"> BeanDefinitionRegistryPostProcessor</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Override</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> postProcessBeanDefinitionRegistry</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">BeanDefinitionRegistry</span><span style="color:#E06C75;font-style:italic"> beanDefinitionRegistry</span><span style="color:#ABB2BF">)</span><span style="color:#C678DD"> throws</span><span style="color:#E5C07B"> BeansException</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"><span style="color:#E5C07B">        BeanDefinition</span><span style="color:#E06C75"> beanDefinitionA</span><span style="color:#56B6C2"> =</span><span style="color:#E5C07B"> beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceA"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        BeanDefinition</span><span style="color:#E06C75"> beanDefinitionB</span><span style="color:#56B6C2"> =</span><span style="color:#E5C07B"> beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">getBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceB"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">removeBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceA"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">removeBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceB"</span><span style="color:#ABB2BF">);</span></span>
<span class="line"><span style="color:#E5C07B">        beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">registerBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceB"</span><span style="color:#ABB2BF">,beanDefinitionA);</span></span>
<span class="line"><span style="color:#E5C07B">        beanDefinitionRegistry</span><span style="color:#ABB2BF">.</span><span style="color:#61AFEF">registerBeanDefinition</span><span style="color:#ABB2BF">(</span><span style="color:#98C379">"serviceA"</span><span style="color:#ABB2BF">,beanDefinitionB);</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    @</span><span style="color:#E5C07B">Override</span></span>
<span class="line"><span style="color:#C678DD">    public</span><span style="color:#C678DD"> void</span><span style="color:#61AFEF"> postProcessBeanFactory</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">ConfigurableListableBeanFactory</span><span style="color:#E06C75;font-style:italic"> configurableListableBeanFactory</span><span style="color:#ABB2BF">)</span><span style="color:#C678DD"> throws</span><span style="color:#E5C07B"> BeansException</span><span style="color:#ABB2BF"> {</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div></div></template>


